3 #include "ScreenNames.h"
4 #include "RescourceKeys.h"
10 FileIO::FileIO(FileIO
* fio
)
15 FileIO::FileIO(ResourceMaster
* rscrc
)
28 void FileIO::storeGameText(std::vector
<std::string
> item
) {
32 void FileIO::saveUserData(int data
, int line
) {
33 std::ifstream
saveFile("data/save.txt");
34 std::ofstream
tempFile("data/temp.txt");
36 std::array
<std::string
,3> currentData
;
38 while (!saveFile
.eof()) {
40 std::getline(saveFile
, buffer
);
41 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
42 currentData
.at(counter
) = buffer
;
43 std::cout
<< buffer
<< "\n";
50 if (line
!= counter
+ 1) {
51 tempFile
<< currentData
.at(counter
) + "\n";
54 tempFile
<< std::to_string(data
) + "\n";
62 std::remove("data/save.txt");
63 std::rename("data/temp.txt", "data/save.txt");
66 void FileIO::saveScoreData(int gamemode
, int data
, int line
) {
67 std::ifstream
saveFile("data/scores.txt");
68 std::ofstream
tempFile("data/temp.txt");
69 std::array
<std::string
, 8> currentData
;
71 while (!saveFile
.eof()) {
73 std::getline(saveFile
, buffer
);
74 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
75 currentData
.at(counter
) = buffer
;
77 std::cout
<< buffer
<< "\n";
83 if (counter
% 2 != gamemode
- 1 || counter
/ 2 + 1 != line
) {
84 tempFile
<< currentData
.at(counter
) + "\n";
87 tempFile
<< std::to_string(data
) + "\n";
95 std::remove("data/scores.txt");
96 std::rename("data/temp.txt", "data/scores.txt");
99 std::vector
<std::string
> FileIO::loadLanguage(std::string path
)
101 std::ifstream langData
;
105 std::vector
<std::string
> result
;
106 while (std::getline(langData
, text
)) {
107 std::cout
<< text
<< "\n";
109 if (text
.length() == 1 && text
.at(0) != ' ') {
110 result
.push_back(text
);
112 else if (text
.at(0) != '/' && text
.at(1) != '/') {
113 result
.push_back(text
);
118 storeGameText(result
);
122 int FileIO::loadUserData(int line
) {
124 std::string storedData
;
126 data
.open("Data/save.txt");
128 std::array
<std::string
, 3> currentData
;
130 while (!data
.eof()) {
132 std::getline(data
, buffer
);
133 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
134 currentData
.at(counter
) = buffer
;
135 std::cout
<< buffer
<< "\n";
142 std::stringstream
convert(currentData
.at(line
- 1));
143 if (!(convert
>> intStoredData
)) {
145 std::cout
<< "err\n";
147 return intStoredData
;
150 int FileIO::loadScoreData(int gamemode
, int line
) {
151 std::string storedData
;
153 data
.open("Data/scores.txt");
154 std::array
<std::string
, 8> currentData
;
157 while (!data
.eof()) {
159 std::getline(data
, buffer
);
160 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
161 currentData
.at(counter
) = buffer
;
170 if (gamemode
== arcade
) {
171 line
= (line
- 1) * 2 + 1;
173 else if (gamemode
== life
) {
178 std::stringstream
convert(currentData
.at(line
- 1));
179 if (!(convert
>> intStoredData
)) {
181 std::cout
<< "err\n";
183 return intStoredData
;
187 void FileIO::loadMenuItems()
189 std::vector<SDL_Texture*> menuTextures;
191 for (int fileName = LangBG; fileName <= GenericBar; fileName++) {
192 SDL_Texture* sdlText = loadTexture("Art/" + std::to_string(fileName + 1) + ".png");
193 if (sdlText == NULL) {
194 std::cout << SDL_GetError() << " Number " << (fileName + 1) << ".png\n";
196 menuTextures.push_back(sdlText);
198 storeTextures(menuTextures);
201 void FileIO::loadMenuAnimations() {
202 std::vector<std::vector<SDL_Texture*>> menuAnimations;
204 for (int animationFolder = WhiteBlueBG; animationFolder <= WhiteBlueBG; animationFolder++) {
205 std::vector<SDL_Texture*> sdlTextList;
206 for (int i = 1; i <= 57; i++) {
207 sdlTextList.push_back(loadTexture("Art/" + std::to_string(animationFolder) + "/ (" + std::to_string(i) + ").png"));
209 if (sdlTextList.front() == NULL) {
210 std::cout << SDL_GetError();
212 menuAnimations.push_back(sdlTextList);
214 storeAnimations(menuAnimations);
218 void FileIO::loadGameAnimations()
220 std::vector< std::vector<SDL_Texture*>> gameAnimations;
223 void FileIO::loadGameGraphics()
225 std::vector<SDL_Texture*> gameGraphics;
228 SDL_Texture* FileIO::loadTexture(std::string path) {
230 SDL_Texture* newTexture = NULL;
231 SDL_Surface* loadedSurface = IMG_Load(path.c_str());
233 newTexture = SDL_CreateTextureFromSurface(rsc->rendPtr, loadedSurface);
234 SDL_FreeSurface(loadedSurface);
242 SDL_Texture
* FileIO::loadSpecificImage(std::string path
, int image
) {
243 return extractSpecificTexture(path
, image
);
245 std::vector
<SDL_Texture
*> FileIO::loadSpecificZip(std::string path
) {
246 return extractTexturesFromZip(path
);
248 void FileIO::loadBGZip() {
249 rsc
->gameBG
= extractTexturesFromZip("BG.zip");
251 void FileIO::loadTexturesZip() {
252 rsc
->gameGraphics
= extractTexturesFromZip("Textures.zip");
254 void FileIO::loadAnimationsZip() {
255 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(0).zip"));
257 for (int i
= 1; i
<= 36; i
++) {
258 rsc
->gameAnimations
.push_back(extractTexturesFromZip(" (" + std::to_string(i
) + ").zip"));
261 void FileIO::loadAnimationsZipUntil(int number
) {
263 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(0).zip"));
266 for (int i
= 1; i
<= number
; i
++) {
267 rsc
->gameAnimations
.push_back(extractTexturesFromZip(" (" + std::to_string(i
) + ").zip"));
271 void FileIO::loadAnimationsZipPast(int number
) {
273 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(0).zip"));
275 for (int i
= number
+ 1; i
<= 36; i
++) {
276 //quick fix for file name issues
278 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(" + std::to_string(i
) + ").zip"));
280 catch (std::exception e
) {
281 rsc
->gameAnimations
.push_back(extractTexturesFromZip(" (" + std::to_string(i
) + ").zip"));
285 void FileIO::loadAnimationsZipInterval(int start
, int end
) {
287 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(0).zip"));
290 for (int i
= start
; i
<= end
; i
++) {
291 //quick fix for file name issues
293 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(" + std::to_string(i
) + ").zip"));
295 catch (std::exception e
) {
296 rsc
->gameAnimations
.push_back(extractTexturesFromZip(" (" + std::to_string(i
) + ").zip"));
300 void FileIO::loadBGMZip() {
301 rsc
->gameBGM
= extractBGMZip("BGM.zip");
303 void FileIO::loadSFXZip() {
304 rsc
->gameSFX
= extractSFXZip("SFX.zip");
307 SDL_Texture
* FileIO::extractSpecificTexture(std::string path
, int image
) {
308 unzFile genericZip
= unzOpen(path
.c_str());
310 unzGetGlobalInfo(genericZip
, &ugi
);
312 SDL_Texture
* specificText
= NULL
;
315 std::cout
<< "Extracting" << "\n";
316 for (i
= 0; i
< ugi
.number_entry
; i
++) {
319 unzGetCurrentFileInfo(genericZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
320 std::string name
= getZipFileName(ufi
, genericZip
);
321 if (name
== std::to_string(image
)) {
322 uInt size_buf
= ufi
.uncompressed_size
;
323 void*buf
= (void*)malloc(size_buf
);
324 int err
= unzOpenCurrentFilePassword(genericZip
, password
);
328 err
= unzReadCurrentFile(genericZip
, buf
, size_buf
);
330 printf("ErrReadZip\n");
332 SDL_RWops
* rw
= SDL_RWFromMem(buf
, size_buf
);
336 SDL_Surface
* surf
= IMG_Load_RW(rw
, 1);
338 std::cout
<< "surf\n";
340 specificText
= SDL_CreateTextureFromSurface(rsc
->rendPtr
, surf
);
341 if (specificText
== NULL
) {
342 std::cout
<< "text\n";
344 SDL_FreeSurface(surf
);
346 unzClose(genericZip
);
349 unzGoToNextFile(genericZip
);
354 std::vector
<Mix_Music
*> FileIO::extractBGMZip(std::string path
) {
355 unzFile bgmZip
= unzOpen(path
.c_str());
357 unzGetGlobalInfo(bgmZip
, &ugi
);
359 std::vector
<Mix_Music
*> rtnVect(ugi
.number_entry
);
360 std::vector
<Mix_Music
*>::iterator it
;
362 SDL_RWops
* rw
= NULL
;
364 std::cout
<< "Extracting" << "\n";
365 for (i
= 0; i
< ugi
.number_entry
; i
++) {
368 unzGetCurrentFileInfo(bgmZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
370 uInt size_buf
= ufi
.uncompressed_size
;
371 //std::cout << size_buf << "\n";
372 void* buf
= (void*)malloc(size_buf
);
373 int err
= unzOpenCurrentFilePassword(bgmZip
, password
);
379 err
= unzReadCurrentFile(bgmZip
, buf
, size_buf
);
381 std::cout
<< "errunzBGM\n";
383 rw
= SDL_RWFromMem(buf
, size_buf
);
388 Mix_Music
* temp
= Mix_LoadMUS_RW(rw
, 1);
390 std::string name
= getZipFileName(ufi
, bgmZip
);
391 char chars
[] = "()-.wav";
393 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
395 // you need include <algorithm> to use general algorithms like std::remove()
396 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
398 int index
= stoi(name
) - 1;
400 std::cout
<< index
<< "\n";
402 it
= rtnVect
.begin() + index
;
403 rtnVect
.at(index
) = temp
;
404 unzGoToNextFile(bgmZip
);
410 std::vector
<Mix_Chunk
*> FileIO::extractSFXZip(std::string path
) {
411 unzFile sfxZip
= unzOpen(path
.c_str());
413 unzGetGlobalInfo(sfxZip
, &ugi
);
415 std::vector
<Mix_Chunk
*> rtrnVector(ugi
.number_entry
);
417 SDL_RWops
* rw
= NULL
;
419 std::cout
<< "Extracting" << "\n";
420 for (i
= 0; i
< ugi
.number_entry
; i
++) {
423 unzGetCurrentFileInfo(sfxZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
425 uInt size_buf
= ufi
.uncompressed_size
;
426 //std::cout << size_buf << "\n";
427 void* buf
= (void*)malloc(size_buf
);
428 int err
= unzOpenCurrentFilePassword(sfxZip
, password
);
434 err
= unzReadCurrentFile(sfxZip
, buf
, size_buf
);
436 std::cout
<< "errunzSFX\n";
438 rw
= SDL_RWFromMem(buf
, size_buf
);
442 Mix_Chunk
* temp
= Mix_LoadWAV_RW(rw
, 1);
443 std::string name
= getZipFileName(ufi
, sfxZip
);
444 char chars
[] = "()-.wav";
446 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
448 // you need include <algorithm> to use general algorithms like std::remove()
449 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
451 int index
= stoi(name
) - 1;
453 std::cout
<< index
<< "\n";
454 rtrnVector
.at(index
) = (temp
);
456 unzGoToNextFile(sfxZip
);
462 std::vector
<SDL_Texture
*> FileIO::extractTexturesFromZip(std::string path
) {
463 unzFile textureZip
= unzOpen(path
.c_str());
465 unzGetGlobalInfo(textureZip
, &ugi
);
467 std::vector
<SDL_Texture
*> rtrnVect(ugi
.number_entry
);
470 std::cout
<< "Extracting" << "\n";
471 for (i
= 0; i
< ugi
.number_entry
; i
++) {
474 unzGetCurrentFileInfo(textureZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
476 uInt size_buf
= ufi
.uncompressed_size
;
477 //std::cout << size_buf << "\n";
478 void* buf
= (void*)malloc(size_buf
);
479 int err
= unzOpenCurrentFilePassword(textureZip
, password
);
483 int err
= unzOpenCurrentFilePassword(textureZip
, "test");
486 err
= unzReadCurrentFile(textureZip
, buf
, size_buf
);
488 std::cout
<< "errunzTxt\n";
490 SDL_RWops
* rw
= SDL_RWFromMem(buf
, size_buf
);
494 SDL_Surface
* surf
= IMG_Load_RW(rw
, 1);
496 std::cout
<< "surf\n";
498 SDL_Texture
* text
= SDL_CreateTextureFromSurface(rsc
->rendPtr
, surf
);
500 std::cout
<< "text\n";
503 std::string name
= getZipFileName(ufi
, textureZip
);
505 char chars
[] = "()-.png";
507 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
509 // you need include <algorithm> to use general algorithms like std::remove()
510 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
512 int index
= stoi(name
) - 1;
514 std::cout
<< index
<< "\n";
516 rtrnVect
.at(index
) = text
;
518 SDL_FreeSurface(surf
);
521 unzGoToNextFile(textureZip
);
523 unzClose(textureZip
);
527 char* FileIO::getZipFileName(unz_file_info ufi
, unzFile uf
) {
528 char *fileName
= (char *)malloc(ufi
.size_filename
+ 1);
529 unzGetCurrentFileInfo(uf
, &ufi
, fileName
, ufi
.size_filename
+ 1, NULL
, 0, NULL
, 0);
530 fileName
[ufi
.size_filename
] = '\0';
534 void FileIO::eraseUserData()
536 saveUserData(1, LevelUnlocks
);
537 saveUserData(0, LevelScores
);
538 saveUserData(0, GameSettings
);
539 for (int line
= 1; line
<= 4; line
++) {
540 std::cout
<< "skipyyy";
541 saveScoreData(arcade
, 0, line
);
542 saveScoreData(life
, 0, line
);
546 std::vector
<int> FileIO::splitInt(std::string str
, char delimiter
) {
547 std::vector
<int> internal
;
548 std::stringstream
ss(str
);
551 while (std::getline(ss
, result
, delimiter
)) {
552 int i
= atoi(result
.c_str());
553 internal
.push_back(i
);
557 std::vector
<std::string
> FileIO::split(std::string str
, char delimiter
) {
558 std::vector
<std::string
> internal
;
559 std::stringstream
ss(str
);
562 while (std::getline(ss
, result
, delimiter
)) {
563 internal
.push_back(result
);